package com.ims.core.doc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ims.core.doc.domain.InvoiceRedRequest;
import com.ims.core.doc.domain.TSalInvoice;
import com.ims.core.doc.service.ITSalInvoiceService;
import com.ims.manager.ImsAsyncManager;
import com.ims.manager.factory.ImsAsyncFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ims.common.annotation.Log;
import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.enums.BusinessType;
import com.ims.common.utils.poi.ExcelUtil;
import com.ims.common.core.page.TableDataInfo;

/**
 * 销项发票Controller
 *
 * @author IMS
 * @date 2024-05-07
 */
@RestController
@RequestMapping("/doc/invoice")
public class TSalInvoiceController extends BaseController
{
    @Autowired
    private ITSalInvoiceService tSalInvoiceService;

    /**
     * 查询销项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(TSalInvoice tSalInvoice)
    {
        startPage();
        List<TSalInvoice> list = tSalInvoiceService.selectTSalInvoiceList(tSalInvoice);
        return getDataTable(list);
    }

    /**
     * 导出销项发票列表
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:export')")
    @Log(title = "销项发票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TSalInvoice tSalInvoice)
    {
        List<TSalInvoice> list = tSalInvoiceService.selectTSalInvoiceList(tSalInvoice);
        ExcelUtil<TSalInvoice> util = new ExcelUtil<TSalInvoice>(TSalInvoice.class);
        util.exportExcel(response, list, "销项发票数据");
    }

    /**
     * 获取销项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tSalInvoiceService.selectTSalInvoiceById(id));
    }


    /**
     * 获取销项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:query')")
    @GetMapping(value = "/fullInfo/{id}")
    public AjaxResult getFullInfo(@PathVariable("id") Long id)
    {
        return success(tSalInvoiceService.selectTSalInvoiceFullInfoById(id));
    }

    /**
     * 获取销项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:query')")
    @GetMapping(value = "/pre/{searchType}/{id}")
    public AjaxResult getPreInfo(@PathVariable("searchType") String searchType, @PathVariable("id") Long id)
    {
        return success(tSalInvoiceService.selectPreTSalInvoiceById(id, searchType));
    }


    /**
     * 获取销项发票详细信息
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:query')")
    @GetMapping(value = "/next/{searchType}/{id}")
    public AjaxResult getNextInfo(@PathVariable("searchType") String searchType, @PathVariable("id") Long id)
    {
        return success(tSalInvoiceService.selectNextTSalInvoiceById(id, searchType));
    }

    /**
     * 新增销项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:add')")
    @Log(title = "销项发票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TSalInvoice tSalInvoice)
    {
        return toAjax(tSalInvoiceService.insertTSalInvoice(tSalInvoice));
    }

    /**
     * 修改销项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:edit')")
    @Log(title = "销项发票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSalInvoice tSalInvoice)
    {
        return toAjax(tSalInvoiceService.updateTSalInvoice(tSalInvoice));
    }

    /**
     * 删除销项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:remove')")
    @Log(title = "销项发票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tSalInvoiceService.deleteTSalInvoiceByIds(ids));
    }


    /**
     * 开票
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:remove')")
    @Log(title = "销项发票开票", businessType = BusinessType.DELETE)
    @GetMapping("/issue/{ids}")
    public AjaxResult issue(@PathVariable Long[] ids)
    {
        tSalInvoiceService.issueTSalInvoiceByIds(ids);
        return success();
    }


    /**
     * 红冲销项发票
     */
    @PreAuthorize("@ss.hasPermi('doc:ininvoice:red')")
    @Log(title = "销项项发票红冲", businessType = BusinessType.UPDATE)
    @GetMapping("/red")
    public AjaxResult red(InvoiceRedRequest invoiceRedRequest)
    {
        return success(tSalInvoiceService.redTSalInvoiceByIdAndReason(invoiceRedRequest.getId(), invoiceRedRequest.getReason()));
    }

    /**
     * 查询销项发票同步
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:sync')")
    @GetMapping("/sync/{ids}")
    public AjaxResult sync(@PathVariable Long[] ids)
    {
        tSalInvoiceService.synctSalInvoiceByIds(ids);
        return success();
    }

    /**
     * 填开发票
     */
    @PreAuthorize("@ss.hasPermi('doc:invoice:write')")
    @Log(title = "填开发票", businessType = BusinessType.INSERT)
    @PutMapping("/write")
    public AjaxResult write(@RequestBody TSalInvoice tSalInvoice)
    {
        return success(tSalInvoiceService.writeAtSalInvoiceAndIssue(tSalInvoice));
    }
}
